package com.woniuxy.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.his.entity.Check;
import com.woniuxy.his.mapper.CheckMapper;
import com.woniuxy.his.service.ICheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.his.vo.AccountProVo;
import com.woniuxy.his.vo.AccountProjectVo;
import com.woniuxy.his.vo.CheckResultVo;
import com.woniuxy.his.vo.InspectVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 检查表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@Service
@Slf4j
public class CheckServiceImpl extends ServiceImpl<CheckMapper, Check> implements ICheckService {

    @Resource
    private CheckMapper checkMapper ;


    /**
     * 过号操作 修改当前检查表里面的排序字段的值
     * @param checkId
     * @return
     */
    @Override
    public boolean updateCheckDetailUpdateTimeByCheckDetailId(Integer checkId) {

        //修改参数的类
        UpdateWrapper<Check> wrapper = new UpdateWrapper<>();
        //将排序字段设置为999，排队到最后
        wrapper.set("sort",999);
        //修改的条件字段
        wrapper.eq("check_id", checkId);
        Integer update = checkMapper.update(null, wrapper);
        return update !=null ? true : false;
    }

    /*
        查询所有已缴费患者检查，并排序
     */
    @Override
    public IPage findAllIspaid(int pageSize,int current) {
        log.info("分页参数：页容量{}，当前页{}",pageSize,current);

        IPage page = new Page(current, pageSize);
        IPage allIspaids = checkMapper.findAllIspaid(page);
        List<InspectVo> records = allIspaids.getRecords();
        //初始化一个变量用来排序
        int numberOf =1;
        for (int i = 1;i<records.size();i++) {

        }
        log.info("数据：{}",records);


        return allIspaids;
    }

    @Override
    public ArrayList<CheckResultVo> queryCheckResult(Integer input) {
        CheckResultVo blood = checkMapper.getBlood(input);
        CheckResultVo pee = checkMapper.getPee(input);
        CheckResultVo image = checkMapper.getImage(input);

        ArrayList<CheckResultVo> arrayList = new ArrayList<>();
        arrayList.add(blood);
        arrayList.add(pee);
        arrayList.add(image);
        return arrayList;
    }

    @Override//项目划价后,传递项目信息,写入检查表和检查详情表
    public Integer accountProject(AccountProjectVo accountProjectVo) {
        //获取病例ID
        Integer caseId = checkMapper.getCaseId(accountProjectVo.getGuaId());
        //插入检查表
        Integer integer = checkMapper.insertCheck(accountProjectVo.getTotalProject(), caseId);

        //获取检查表ID
        Integer checkId = checkMapper.getCheckId(caseId);
        //遍历集合获取单价，项目名
        ArrayList<AccountProVo> tableChecks = accountProjectVo.getTableCheck();//项目集合
        ArrayList<String>  itemNames= new ArrayList<>();//项目名集合
        for (AccountProVo apv:tableChecks){
            itemNames.add(apv.getItemName());
        }
        //动态获取项目ID集合
        List<Integer> itemIds = checkMapper.getItemId(itemNames);
        //插入检查详情表
        int i=0;

        for (AccountProVo apv:tableChecks){
            checkMapper.insertCheckDetail(apv.getItemName(), accountProjectVo.getDoctorId(), apv.getPrice(), checkId, itemIds.get(i));
            i++;
        }
        return i;
    }

    //修改检查项目付费情况并添加序列号
    @Override
    public Integer checkPay(Integer sortNum,Integer checkId) {
        UpdateWrapper<Check> checkUpdateWrapper = new UpdateWrapper<>();
        checkUpdateWrapper.set("sort",sortNum).set("ispaid","yes").eq("check_id",checkId);
        int num = checkMapper.update(null, checkUpdateWrapper);
        return num;
    }
}
